package com.park.web.controller.admin;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.park.client.model.pub.SensitiveWordModel;
import com.park.client.service.pub.SensitiveWordService;
import com.park.common.util.Page;
import com.park.common.util.PageData;
import com.park.common.util.Result;
import com.park.web.contant.Global;
import com.park.web.controller.BaseController;

@Controller
@RequestMapping("/admin/word")
public class AdminSensitiveWordController extends BaseController{

	@Autowired
	private SensitiveWordService sensitiveWordService;
	
	@RequestMapping("listRows")
	@ResponseBody
	public PageData<SensitiveWordModel> listRows(Integer rows,@RequestParam(name="page",defaultValue="1")Integer pageNow,HttpServletRequest request){
		PageData<SensitiveWordModel> pageData = new PageData<>();
		SensitiveWordModel sensitiveWord = new SensitiveWordModel();
		Integer total = sensitiveWordService.selectTotal(sensitiveWord);
		Page page = new Page(pageNow,total,rows);
		sensitiveWord.setStartPos(page.getStartPos());
		sensitiveWord.setPageSize(page.getPageSize());
		List<SensitiveWordModel> list = sensitiveWordService.selectPagination(sensitiveWord);
		pageData.setTotal(total);
		pageData.setRows(list);
		return pageData;
	}
	
	@RequestMapping("wordList")
	public String linkList(HttpServletRequest request){
			return Global.ADMIN_PATH+"word/wordList";
	}
	
	@RequestMapping("save")
	@ResponseBody
	public Result<String> save(SensitiveWordModel sensitiveWord,HttpServletRequest request){
		Result<String> json = new Result<>();
		sensitiveWord.setCreateDate(new Date());
		boolean success = sensitiveWordService.insertSelective(sensitiveWord);
		if(success){
			json.setMessage("添加成功!");
			json.setSuccess(true);
		}else{
			json.setSuccess(false);
			json.setMessage("添加失败!");
		}
		return json;
	}
	
	@RequestMapping("deleteById")
	@ResponseBody
	public Result<String> deleteById(@RequestParam("id") Long id,HttpServletRequest request){
		Result<String> json = new Result<>();
		boolean success = sensitiveWordService.deleteByPrimaryKey(id);
		if(success){
			json.setMessage("删除成功!");
			json.setSuccess(true);
		}else{
			json.setMessage("删除失败!");
			json.setSuccess(false);
		}
		return json;
	}
	
	@RequestMapping("edit")
	@ResponseBody
	public Result<String> edit(SensitiveWordModel sensitiveWord,HttpServletRequest request){
		Result<String> json = new Result<>();
		sensitiveWord.setCreateDate(new Date());
		boolean success = sensitiveWordService.updateByPrimaryKeySelective(sensitiveWord);
		if(success){
			json.setMessage("修改成功!");
			json.setSuccess(true);
		}else{
			json.setMessage("修改失败!");
			json.setSuccess(false);
		}
		return json;
	}
}
